package org.hot100.h238;

import java.util.Arrays;

/**
 * @Author: wjy
 * @Date: 2022/2/14 14:04
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {1, 2, 3, 4};

        Solution solution = new Solution();
        int[] res = solution.productExceptSelf(ints);

        System.out.println(Arrays.toString(res));

    }

    public int[] productExceptSelf(int[] nums) {
        if (nums.length == 0) {
            return nums;
        }
        
        int[] res = new int[nums.length];
        int p = 1, q = 1;

        for (int i = 0; i < nums.length; i++) {
            res[i] = p;
            p *= nums[i];
        }

        for (int i = nums.length - 1; i > 0; i--) {
            q *= nums[i];
            res[i - 1] *= q;
        }

        return res;
    }

}
